package guoanmaker.operator.business.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.OperatorActivityEnlist;

/**
 * @author wanghongwei
 * @ClassName: OperatorActivityEnlistRepository 
 * @Description: OperatorActivityEnlistRepository
 * @date 2017/8/11
 */
public interface OperatorActivityEnlistRepository extends JpaRepository<OperatorActivityEnlist, String>, JpaSpecificationExecutor<OperatorActivityEnlist>{
	@Query("select oae from OperatorActivityEnlist oae where oae.userId=:userId and oae.activityId=:activityId")
	public OperatorActivityEnlist  queryActivityEnlistByuserId(@Param("userId")String userId,@Param("activityId")String activityId);
	@Transactional
	@Modifying
	@Query("UPDATE OperatorActivityEnlist set state=:state  where id=:id and state=:orgState")
	public int updateActivityEnlistState(@Param("state")Integer state,@Param("orgState")Integer orgState,@Param("id")String id)throws Exception;
	@Transactional
	@Modifying
	@Query("UPDATE OperatorActivityEnlist set buildTime=NOW() where id=:id")
	public  void updateBuildTime(@Param("id")String id);
	@Transactional
	@Modifying
	@Query("UPDATE OperatorActivityEnlist set couponId=:couponId , couponPrice=:couponPrice where id=:id")
	public  void updateCouponIdAndPrice(@Param("id")String id,@Param("couponId")String couponId,@Param("couponPrice")Double couponPrice);
	@Transactional
	@Modifying
	@Query("UPDATE OperatorActivityEnlist set streamNum=:streamNum  where id=:id")
	public  int  updateStreamNum(@Param("id")String id,@Param("streamNum")String streamNum);
	@Query("select count(oae.id) from OperatorActivityEnlist  oae  where oae.userId=:userId and oae.activityId=:activityId and (oae.state=0 or oae.state=1) ")
    public int queryCountByUserIdAndActivity(@Param("userId")String userId,@Param("activityId")String activityId);
	@Query("select count(oae.id) from OperatorActivityEnlist oae where oae.userId=:userId ")
	public int queryCountByUserId(@Param("userId")String userId);

}
